 package se.hh.lab.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import se.hh.lab.dao.DaoException;
import se.hh.lab.dao.UserDao;
import se.hh.lab.domain.User;
import se.hh.lab.jdbc.JdbcUtil;

public class UserDaoImpl implements UserDao
{

	@SuppressWarnings("finally")
	@Override
	public int updateUser(User user) 
	{
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs =null;	
		int mark=0;
		try
		{		
			con = JdbcUtil.getConnection();		
			String sql="update lab_user set firstname=?,lastname=?,gender=?,age=?,email=?," +
					"phone=?,remark=? where userID=?" ;		
			ps = con.prepareStatement(sql);	
			ps.setString(1,user.getFirstname());
			ps.setString(2,user.getLastname());
			ps.setString(3,user.getGender());
			ps.setString(4,user.getAge());
			ps.setString(5,user.getEmail());
			ps.setString(7,user.getPhone());
			ps.setString(8,user.getRemark());
			ps.setString(9,user.getUserID());
			mark=ps.executeUpdate();			
		}	
		catch (Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}
		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			return mark;
		}			
	}

	

	
	public ArrayList<User> getUser(User user) 
	{
			Connection con = null;
			PreparedStatement ps = null;
			ResultSet rs =null;		
			String sql=null;			
			ArrayList<User> list=new ArrayList<User>();
			try
			{				
				if(user.getUserID()!=null)
				{
					sql="select userID,firstname,lastname,gender,age,email,phone,remark,date from lab_user" +
							" where userID ='"+user.getUserID()+" '";
				}
				else if(user.getFirstname()!=null)
				{
					sql="select userID,firstname,lastname,gender,age,email,phone,remark,date from lab_user" +
							" where firstname like '%"+user.getFirstname()+"%'";
				}
				else 
				{
					sql="select userID,firstname,lastname,gender,age,email,phone,remark,date from lab_user" +
							" where lastname like '%"+user.getLastname()+"%'";
				}

				con = JdbcUtil.getConnection();						
				ps = con.prepareStatement(sql);
				rs=ps.executeQuery();
				while (rs.next())
				{
					user=new User();
					user.setUserID(rs.getString("userID"));
					user.setFirstname(rs.getString("firstname"));
					user.setLastname(rs.getString("lastname"));
					user.setGender(rs.getString("gender"));
					user.setAge(rs.getString("age"));
					user.setEmail(rs.getString("email"));
					user.setPhone(rs.getString("phone"));	
					user.setRemark(rs.getString("remark"));
					user.setDate(rs.getDate("date"));
					list.add(user);
				}
			}	
			catch (Exception e)
			{
				throw new DaoException(e.getMessage(),e);
			}

			finally
			{
				try
				{
					JdbcUtil.free(rs, ps, con);
				}
				catch (SQLException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}			
		return list;
	}

	
}
